<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>在线购物登陆页面</title>
  <link rel="stylesheet" href="./css/login.css">
  <script src="./js/login.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f2f2f2;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .container {
      width: 80%;
      max-width: 400px;
      /* 可根据需要进行调整 */
    }

    .login-form {
      background-color: #fff;
      padding: 20px;
      border-radius: 5px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }

    .form-group input {
      width: 100%;
      padding: 8px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    .form-group .button-group {
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }

    .form-group button {
      padding: 8px 16px;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      margin-left: 10px;
    }

    #login {
      background-color: green;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    .form-group .btn-clear {
      background-color: #ccc;
    }

    .form-group .btn-clear.valid {
      background-color: #4CAF50;
    }
  </style>
</head>

<body>
  <div class="container">
    <form id="loginForm" action="login" method="post">

      <h2>在线购物登录</h2>
      <div class="form-group">
        <label for="phone_number">用户名:</label>
        <input type="text" id="phone_number" name="phone_number" required>
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
      </div>
      <div class="form-group">
        <div class="button-group">
          <button type="button" class="btn-clear">清空</button>
          <button id="login" type="button">登录</button>
        </div>
      </div>

    </form>
  </div>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    const btnClear = document.querySelector('.btn-clear');
    const phone_numberInput = document.getElementById('phone_number');
    const passwordInput = document.getElementById('password');

    function checkInputs() {
      const phone_numberValue = phone_numberInput.value.trim();
      const passwordValue = passwordInput.value.trim();

      // 检查输入框的值并添加/移除 valid 类名
      if (phone_numberValue !== '' || passwordValue !== '') {
        btnClear.classList.add('valid');
      } else {
        btnClear.classList.remove('valid');
      }
    }

    // 在输入框值改变时调用 checkInputs
    phone_numberInput.addEventListener('input', checkInputs);
    passwordInput.addEventListener('input', checkInputs);

    // 点击清空按钮时清空输入框的值
    btnClear.addEventListener('click', function () {
      phone_numberInput.value = '';
      passwordInput.value = '';
      // 移除 valid 类名
      btnClear.classList.remove('valid');
    });
    // 点击登录按钮时发送Ajax请求
    const loginButton = document.getElementById('login');

    loginButton.addEventListener('click', function () {
      const phone_number = phone_numberInput.value.trim();
      const password = passwordInput.value.trim();
      if (phone_number !== '' && password !== '') {
        $.ajax({
          type: 'POST',
          url: 'login',
          data: {
            phone_number: phone_number,
            password: password
          },
          success: function (body) {
            if (body.code == 200) {  // 登陆成功，跳转到首页
              window.location.href = 'home.html';
            } else {  // 登录失败，打印提示信息。
              alert(body.msg)
            }
          },
          error: function () {
            console.error('Error: Login request failed');
          }
        });
      }
    });
  </script>
</body>

</html>